﻿/******************************************************************************
 * 
 * Announce: CSharpKit, Basic algorithms, components and definitions.
 *           Copyright (C) ShenYongchen.
 *           All rights reserved.
 *   Author: 申永辰.郑州 (shenyczz@163.com)
 *  WebSite: http://github.com/shenyczz/CSharpKit
 *
 * THIS CODE IS LICENSED UNDER THE MIT LICENSE (MIT).
 * THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF 
 * ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
 * IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
 * PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
 * 
******************************************************************************/

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace CSharpKit.Numerics.LinearAlgebra
{
    /// <summary>
    /// 对角线矩阵存储
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [Serializable]
    public class MatrixStorageDiagonal<T> : MatrixStorage<T>
        where T : struct, IFormattable, IEquatable<T>
    {
        #region Constructors

        public MatrixStorageDiagonal(int row, int column)
            : base(row, column)
        {
            _IsDense = false;
            _IsMutableFully = false;

            _Order = MajorOrders.Diagonal;
        }

        #endregion
        

        [DataMember(Order = 1)]
        public readonly T[] Data;



        #region Indexer - 索引器

        internal override T At(int row, int column)
        {
            return row == column ? Data[row] : Zero;
        }

        internal override void At(int row, int column, T tvalue)
        {
            if (row == column)
            {
                Data[row] = tvalue;
            }
            else if (!Zero.Equals(tvalue))
            {
                throw new IndexOutOfRangeException("Cannot set an off-diagonal element in a diagonal matrix.");
            }
        }

        public override bool IsMutableAt(int row, int column)
        {
            return row == column;
        }

        #endregion





        //}}@@@
    }


}
